package com.firewolf.oa.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.firewolf.oa.base.BaseAction;
import com.firewolf.oa.entities.Permission;
import com.firewolf.oa.entities.Role;
import com.opensymphony.xwork2.ActionContext;

/**
 * 角色管理Action
 * 
 * @author liuxing
 *
 */
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role> {

	private static final long serialVersionUID = -1312776324900840832L;
	
	/** 当前角色具有的权限组id，用于回显 */
	private Long[]  permissionIds;
	/**
	 * 跳转到设置权限的页面
	 * @return
	 */
	public String toSetPermission(){
		try {
			//准备权限数据
			//0.查询当前角色信息
			Role role = roleService.findById(model.getId());
			ActionContext.getContext().getValueStack().push(role);//放到栈顶
			
			//1.获取顶级权限数据
			List<Permission> pList = permissionService.findTopPermission();
			ActionContext.getContext().put("pList", pList);
			//2.查询当前角色具有的权限，用于回显
			Set<Permission> permissions = role.getPermissions();
			if(permissions!=null && permissions.size() > 0){
				permissionIds = new Long[permissions.size()];
				int i = 0;
				for(Permission p : permissions){
					permissionIds[i++] = p.getId();
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "setpermission";
	}

	/**
	 * 设置权限
	 * @return
	 * @throws Exception 
	 */
	public String setpermission(){
		try {
			List<Permission> permissions = permissionService.getByIds(permissionIds);
			Role r = roleService.findById(model.getId());
			r.setPermissions(new HashSet<>(permissions));
			roleService.update(r);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "toList";
	}
	
	/**
	 * 去新增页面
	 * 
	 * @return
	 */
	public String toAdd() {
		return "save";
	}

	/**
	 * 去修改页面
	 * 
	 * @return
	 */
	public String toUpdate() {
		try {
			Role role = roleService.findById(model.getId());
			ActionContext.getContext().getValueStack().push(role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "save";
	}

	/**
	 * 去列表页面
	 * 
	 * @return
	 */
	public String list() {
		try {
			List<Role> roleList = roleService.findAll();
			ActionContext.getContext().put("roleList", roleList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "list";
	}

	/**
	 * 添加
	 * 
	 * @return
	 */
	public String add() {
		try {
			roleService.save(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toList";
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String update() {
		try {
			roleService.update(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toList";
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		try {
			roleService.delete(model.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toList";
	}

	public Long[] getPermissionIds() {
		return permissionIds;
	}

	public void setPermissionIds(Long[] permissionIds) {
		this.permissionIds = permissionIds;
	}

}
